# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=protobuf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=21.12
pkgrel=1
pkgdesc="Protocol Buffers - Google's data interchange format (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://developers.google.com/protocol-buffers/'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' '!debug' 'staticlibs')
source=(https://github.com/protocolbuffers/${_realname}/releases/download/v${pkgver}/${_realname}-all-${pkgver}.tar.gz
        0001-protobuf-3.1.0-gcc6.2.0-tests.patch
        0002-windres-invocation.patch)
sha256sums=('2c6a36c7b5a55accae063667ef3c55f2642e67476d96d355ff0acb13dbb47f09'
            'b71354ef179da2d632c9149addc3994ce38234091994dd35f602eb12c8e1a14e'
            '174f714b842d5153c79c5fda1ae775ee002aea11d53cb5d5f51cb5c4b9e63d29')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i ${srcdir}/0001-protobuf-3.1.0-gcc6.2.0-tests.patch
  patch -p1 -i ${srcdir}/0002-windres-invocation.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  echo "Building static library"
  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${MSYSTEM}-static"
  cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -Wno-dev \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -Dprotobuf_BUILD_SHARED_LIBS="OFF" \
      -Dprotobuf_BUILD_TESTS="OFF" \
      -Dprotobuf_WITH_ZLIB="ON" \
      ../${_realname}-${pkgver}/cmake

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  echo "Building shared library"
  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${MSYSTEM}-shared"
  cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -Wno-dev \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -Dprotobuf_BUILD_SHARED_LIBS="ON" \
      -Dprotobuf_BUILD_TESTS="OFF" \
      -Dprotobuf_WITH_ZLIB="ON" \
      ../${_realname}-${pkgver}/cmake

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}/${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
